package com.example.msn_client;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    private static String TAG = "msn-client";
    private Button mButton;
    private EditText mEditText, mEditText2;

    private Messenger mClientMessenger = new Messenger(new MyClientHandler());

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mButton = findViewById(R.id.btn);
        mEditText = findViewById(R.id.ed);
        mEditText2 = findViewById(R.id.ed2);
        mButton.setOnClickListener( v -> {
            int val = Integer.parseInt(mEditText.getText().toString());
            int val2 = Integer.parseInt(mEditText2.getText().toString());
            sendInt(val, val2);
        });
    }

    private void sendInt(int v, int v2) {
        if (mMessenger == null) return;
        Log.i(TAG, "sendInt, v:" + v + ", v2:" + v2);
        Message msg = Message.obtain();
        msg.what = 1;
        msg.arg1 = v;
        msg.arg2 = v2;
        msg.replyTo = mClientMessenger;
        try {
            mMessenger.send(msg);
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        bindService();
    }

    @Override
    protected void onStop() {
        super.onStop();
        unBindService();
    }

    private Messenger mMessenger;

    private final ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.i(TAG, "onServiceConnected, cn name:" + name);
            mMessenger = new Messenger(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i(TAG, "onServiceDisconnected, cn name:" + name);
            mMessenger = null;
        }
    };

    private void bindService() {
        Log.i(TAG, "bindService");
        Intent i = new Intent();
        i.setPackage("com.example.msn_server");
        i.setAction("example.msn.server");
        getApplicationContext().bindService(i, conn, Context.BIND_AUTO_CREATE);
    }

    private void unBindService() {
        Log.i(TAG, "unBindService");
        getApplicationContext().unbindService(conn);
    }

    static class MyClientHandler extends Handler {
        @Override
        public void handleMessage(@NonNull Message msg) {
            Log.i(TAG, "MyClientHandler, handleMessage, msg:" + msg);
        }
    }
}